package securityproject.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class RuleUtils {

	public static final String RULE_SCOPE = "ruleScope";

	public static final String GLOBAL = "global";

	public static final String DELETE_PREFIX = "_to_del";

	public static String getRuleFolder(String ruleScope, boolean active) {

		if (active) {
			return Constants.XSL_FILTER_RULES_FOLDER + ruleScope + "/"
					+ Constants.XSL_ACTIVE_FOLDER;
		} else {
			return Constants.XSL_FILTER_RULES_FOLDER + ruleScope + "/"
					+ Constants.XSL_INACTIVE_FOLDER;
		}

	}

	public static String getXmlRule(String ruleName) {
		return Constants.XML_RULES_FOLDER + ruleName + ".xml";
	}

	public static void writeXML(String fileName, Document document)
			throws TransformerFactoryConfigurationError,
			TransformerConfigurationException, IOException,
			TransformerException {

		// delete xml file if exists
		String rule = RuleUtils.getXmlRule(fileName);
		File ruleFile = new File(rule);
		if (ruleFile.exists()) {
			ruleFile.delete();
		}

		// set up a transformer
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans = transfac.newTransformer();
		trans.setOutputProperty(OutputKeys.INDENT, "yes");

		// create string from xml tree
		FileWriter fileWriter = new FileWriter(rule);
		StreamResult result = new StreamResult(fileWriter);
		DOMSource source = new DOMSource(document);
		trans.transform(source, result);

		fileWriter.flush();
		fileWriter.close();
	}

	public static String createCustomXMLRule(String ruleName)
			throws TransformerFactoryConfigurationError, IOException {
		try {

			DocumentBuilder dBuilder;
			// Instantiate a DocumentBuilderFactory.
			javax.xml.parsers.DocumentBuilderFactory dfactory = DocumentBuilderFactory
					.newInstance();
			dBuilder = dfactory.newDocumentBuilder();

			Document document = dBuilder.newDocument();

			// create base elemnt

			Element rule = document.createElement("rule");
			document.appendChild(rule);

			// set rule name

			Element name = document.createElement("name");
			rule.appendChild(name);

			Text nameText = document.createTextNode(ruleName);
			name.appendChild(nameText);

			// set rule description

			Element description = document.createElement("description");
			rule.appendChild(description);

			// set rule type

			Element ruleType = document.createElement("ruleType");
			rule.appendChild(ruleType);

			Text ruleTypeText = document.createTextNode("custom");
			ruleType.appendChild(ruleTypeText);

			// set rule tag name to unknown

			Element tagName = document.createElement("tagName");
			rule.appendChild(tagName);

			Text tagNameText = document.createTextNode("");
			tagName.appendChild(tagNameText);

			RuleUtils.writeXML(ruleName, document);

			return ruleName;

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;

		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;

		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;

		}
	}

	public static String getRuleName(String fileName) {
		int filterTypeIndex = fileName.indexOf("_");
		int ruleTypeIndex = fileName.indexOf("_", filterTypeIndex + 1);

		int extentionIndex = fileName.lastIndexOf(".");

		return fileName.substring(ruleTypeIndex + 1, extentionIndex);
	}

	public static String getRuleType(String fileName) {
		int filterTypeIndex = fileName.indexOf("_");

		if (filterTypeIndex == -1) {
			return "custom";
		}

		int ruleTypeIndex = fileName.indexOf("_", filterTypeIndex + 1);

		if (ruleTypeIndex == -1) {
			return "custom";
		}

		return fileName.substring(filterTypeIndex + 1, ruleTypeIndex);
	}

	public static String getFilterType(String fileName) {
		int filterTypeIndex = fileName.indexOf("_");

		if (filterTypeIndex == -1) {
			return "undefined";
		}

		return fileName.substring(0, filterTypeIndex);
	}

}
